package com.netty.service;

import com.netty.constant.Constants;
import com.netty.helper.MessageFactory;
import com.netty.model.Message;
import com.netty.model.MessageBody;
import com.netty.model.MessageWrapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * @author suzhen
 * @create 2018/7/13
 */
@Service
@Slf4j
public class MessageServiceImpl implements MessageService {

    @Resource
    private RobotService robotService;

    @Override
    public MessageWrapper convertToMessageWrapper(String sessionId, Message message) {
        switch (message.getCmd()) {
            case Constants.CmdType.BIND:
                log.debug("MessageService: bind");
                try {
                    return new MessageWrapper(MessageWrapper.MessageProtocol.CONNECT, message.getSender(), null, message);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            case Constants.CmdType.HEARTBEAT:
                log.debug("MessageService: HEARTBEAT");
                try {
                    return new MessageWrapper(MessageWrapper.MessageProtocol.HEART_BEAT, sessionId, null, null);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            case Constants.CmdType.ONLINE:
                break;
            case Constants.CmdType.OFFLINE:
                break;
            case Constants.CmdType.MESSAGE:
                log.debug("MessageService: MESSAGE");
                try {
                    message.setTimeStamp(LocalDateTime.now().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME));
                    //存入发送人sessionId
                    message.setSender(sessionId);
                    //判断消息是否有接收人
                    if (StringUtils.isNotEmpty(message.getReceiver())) {
                        //判断是否发消息给机器人
                        if (message.getReceiver().equals(Constants.ImserverConfig.REBOT_SESSIONID)) {
                            MessageBody msg = MessageFactory.createMessageBody(message);
                            //机器人操作
                            //介入第三方聊天程序
                            return robotService.botMessageReply(sessionId, msg.getContent());
                        } else {
                            return new MessageWrapper(MessageWrapper.MessageProtocol.REPLY, sessionId, message.getReceiver(), message);
                        }
                    } else if (StringUtils.isNotEmpty(message.getGroupId())) {
                        log.debug("MessageService: MESSAGE getGroupId");
                        return new MessageWrapper(MessageWrapper.MessageProtocol.GROUP, sessionId, null, message);
                    } else {
                        log.debug("MessageService: MESSAGE SEND");
                        return new MessageWrapper(MessageWrapper.MessageProtocol.SEND, sessionId, "", message);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            default:
                break;
        }
        return null;
    }

    @Override
    public void saveOnlineMessageToDb(MessageWrapper message) {
        //保存到数据库
    }

    @Override
    public void saveOfflineMessageToDb(MessageWrapper message) {
        //保存到数据库

    }

    @Override
    public Message getOnLineStateMsg(String sessionId) {
        //保存到数据库
        Message message = MessageFactory.createMessage(sessionId);
        message.setTimeStamp(LocalDateTime.now().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME));
        message.setCmd(Constants.CmdType.ONLINE);
        return message;
    }

    @Override
    public MessageWrapper getReConnectionStateMsg(String sessionId) {
        Message message = MessageFactory.createMessage(sessionId);
        message.setTimeStamp(LocalDateTime.now().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME));
        message.setCmd(Constants.CmdType.RECON);
        return new MessageWrapper(MessageWrapper.MessageProtocol.SEND, sessionId, null, message);
    }

    @Override
    public Message getOffLineStateMsg(String sessionId) {
        Message message = MessageFactory.createMessage(sessionId);
        message.setTimeStamp(LocalDateTime.now().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME));
        message.setCmd(Constants.CmdType.OFFLINE);
        return message;
    }
}
